package ahjava.io;
import java.nio.ByteBuffer;
public class NIO01 {
	public static void showBuffer(ByteBuffer bb) {
		System.out.println("容量" + bb.capacity() + ",上限" + bb.limit() + ",位置"
				+ bb.position());
	}
	public static void main(String[] args) {
		System.out.println("===从堆中分配缓冲区===");
		ByteBuffer bb = ByteBuffer.allocate(15);
		showBuffer(bb);
		System.out.println("===put：写入缓冲区===");
		for (int i = 0; i < 10; i++) {
			bb.put((byte) i);
		}
		showBuffer(bb);
		System.out.println("===flip：===");
		// flip():limit=position，position归零，用于读写转换
		bb.flip();// flip：翻转
		showBuffer(bb);
		System.out.println("===get===");
		for (int i = 0; i < 5; i++) {
			bb.get();
		}
		showBuffer(bb);
		System.out.println("===flip===");
		bb.flip();// flip：翻转，flip():重置position
		showBuffer(bb);
	}
}
